package ircbot.app;

import java.io.*;
import java.net.*;
import java.util.regex.*;
import java.util.Date;

public class IRC_Bot{
    
    public static void main( String[] args ) throws IOException
    {
        //connection variables
        String server = "irc.snappeh.com";
        String nick = "KBot";
        String login = "KBot";
        String channel = "#bots";
        int port = 6667;
        
        //for security
        String owner = "lkc";
        
        try {
            
            //our socket we're connected with
            Socket irc = new Socket( server, port );
            //out output stream
            BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( irc.getOutputStream() ) );
            //our input stream
            BufferedReader br = new BufferedReader( new InputStreamReader( irc.getInputStream() ) );
            
            //authenticate with the server
            bw.write( "NICK " + nick + "\n" );
            bw.write( "USER " + login + " java.sun.com BOT: KUHN\n");
            bw.flush();
            
            
            
            //join a channel
            bw.write( "JOIN " + channel + "\n" );
            bw.write( "PRIVMSG " + channel + " :sup!\n" );
            bw.flush();
            
            System.out.println( "Successfully connected to IRC" );
            
            String currLine = null;
            while( ( currLine = br.readLine() ) != null )
            {
                //checks for PING, if one is found; return a PONG
                Pattern pingRegex = Pattern.compile( "^PING", Pattern.CASE_INSENSITIVE ); 
                Matcher ping = pingRegex.matcher( currLine );
                if( ping.find() )
                {
                    bw.write( "PONG " + channel + "\n" );
                    bw.flush();
                }
                
                
                //check for ownership
                Pattern checkOwner = Pattern.compile( "^:"+owner, Pattern.CASE_INSENSITIVE );
                Matcher ownership = checkOwner.matcher( currLine );
                
                //!exit - quit current irc room
                Pattern exitRegex = Pattern.compile( "!exit", Pattern.CASE_INSENSITIVE );
                Matcher exit = exitRegex.matcher( currLine );
                if( exit.find() && ownership.find() )
                {
                    bw.write( "PRIVMSG " + channel + " :Bye Bye\n" );
                    bw.write( "PART " + channel + "\n" );
                    bw.flush();
                    irc.close();
                }
                
                //!time - return current time
                Pattern timeRegex = Pattern.compile( "!time", Pattern.CASE_INSENSITIVE );
                Matcher time = timeRegex.matcher( currLine );
                if( time.find()  && ownership.find() )
                {
                    Date d = new Date();
                    bw.write( "PRIVMSG " + channel + " :" + d +"\n" );
                    bw.flush();
                }
                
                //!sayhi - shows a little message saying hello
                Pattern helloRegex = Pattern.compile( "!sayhi", Pattern.CASE_INSENSITIVE );
                Matcher hello = helloRegex.matcher( currLine );
                if( hello.find()  && ownership.find() )
                {
                    bw.write( "PRIVMSG " + channel + " :Hello, I'm a JavaBot. I was coded by dydx in Java!\n");
                    bw.flush();
                }
                
                //!join <room> - changes to a new room and sets the variables accordingly
                Pattern joinRegex = Pattern.compile( "!join", Pattern.CASE_INSENSITIVE );
                Matcher join = joinRegex.matcher( currLine );
                if( join.find()  && ownership.find() )
                {
                    String[] token = currLine.split( " " );
                    bw.write( "PRIVMSG " + channel + " :Im going over to " + token[4] + "\n" );
                    bw.write( "PART " + channel + "\n" );
                    channel = token[4];
                    bw.write( "JOIN " + channel + "\n" );
                    bw.flush();
                }
            }
        } catch ( UnknownHostException e ) {
            System.err.println( "No such host" );
        } catch ( IOException e ) {
            System.err.println( "There was an error connecting to the host" );
        } 
    }
}